#import "RNAisle.h"
#import "RNItem.h"
#import "RNCategory.h"
#import "RNListItem.h"
#import "NSManagedObject+SRAdditions.h"
#import "RNStore.h"
#import "RNConstants.h"

static BOOL showsChildren;
static id currentListObject;

@implementation RNAisle 

@dynamic title;
@dynamic store;
@dynamic items;
@dynamic categories;
@dynamic listItems;
@dynamic aisleNumber;

+(BOOL)showsChildren {
    return showsChildren;
}

+(void)setShowsChildren:(BOOL)shouldShowChildren {
    showsChildren = shouldShowChildren;
}

+(void)setCurrentListObject:(id)object {
    currentListObject = object;
}

+(id)currentListObject {
    return currentListObject;
}

+(NSSet *)keyPathsForValuesAffectingStoreAndAisleDisplayString {
    return [NSSet setWithObjects:@"store", @"store.title", @"title", nil];
}

-(NSString *)storeAndAisleDisplayString {
    return [NSString stringWithFormat:@"%@: %@…", [[self store] valueForKeyPath:@"title"], [self title]];
}

-(NSDictionary *)objectDictionary {
    // TODO: MAKE THE KEYS ARRAY STATIC
    NSArray *keys = [NSArray arrayWithObjects:@"title", @"itemsCount", nil];
    return [self dictionaryWithValuesForKeys:keys];
}

-(NSNumber *)itemsCount {
    return [NSNumber numberWithInteger:[[self items] count]];
}

+(NSSet *)keyPathsForValuesAffectingChildren {
    return [NSSet setWithObject:@"listItems"];
}

-(NSSet *)children {
    if (!showsChildren)
        return nil;
//    NSSet *children = [self listItems];
    NSSet *filteredChildren = [[self listItems] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"list == %@", currentListObject]];
    return filteredChildren;
}

-(NSDate *)loggedDate {
    return [NSDate date];
}

-(id)parent {
    return [self store];
}

-(NSUInteger)displayIndex {
    return 0;
}

-(NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionary];
    [dictionaryRepresentation setValue:[self title] forKey:@"title"];
    [dictionaryRepresentation setValue:[[self store] URIRepresentation] forKey:@"store"];
    NSMutableSet *categoryURIs = [NSMutableSet set];
    for (NSManagedObject *category in [self categories]) 
        [categoryURIs addObject:[category URIRepresentation]];
    [dictionaryRepresentation setValue:categoryURIs forKey:@"categories"];
    NSMutableSet *itemURIs = [NSMutableSet set];
    for (NSManagedObject *item in [self items])
        [itemURIs addObject:[item URIRepresentation]];
    [dictionaryRepresentation setValue:itemURIs forKey:@"items"];
    NSMutableSet *listItemURIs = [NSMutableSet set];
    for (NSManagedObject *listItem in [self listItems])
        [listItemURIs addObject:[listItem URIRepresentation]];
    [dictionaryRepresentation setValue:listItemURIs forKey:@"listItems"];
    return dictionaryRepresentation;
}

-(void)moveToContainer:(NSManagedObject *)container {
    if (![container isKindOfClass:[RNStore class]]) 
        return;
    
    RNStore *destinationStore = (RNStore *)container;
    NSSet *storeAisles = [destinationStore aisles];
    NSSet *duplicates = [storeAisles filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"title LIKE %@", [self title]]];
    if ([duplicates count] > 0)
        [self setTitle:[NSString stringWithFormat:@"%@ - copy", [self title]]];
    [self setAisleNumber:[NSNumber numberWithInteger:[storeAisles count]]];
    [self setStore:(RNStore *)container];
}

-(BOOL)canContainObjectsWithEntityNames:(NSArray *)entityNames {
    NSArray *validEntities = [entityNames filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self IN { %@, %@ }", RNEntityNameItem, RNEntityNameListItem]];
    return [validEntities count] == [entityNames count];
}

@end
